package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.SysRole;
import com.juzi.wopu.domain.SysUser;
import com.juzi.wopu.domain.SysUserResponse;
import com.juzi.wopu.domain.SysUserRole;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.query.SysUserQuery;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysRoleService;
import com.juzi.wopu.service.ISysUserRoleService;
import com.juzi.wopu.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class SysUserAction extends CRUDAaction<SysUser> {
	
	private static final long serialVersionUID = 1L;
	private ISysUserService sysUserService;
	private ISysCodeService sysCodeService;
	private ISysRoleService sysRoleService;
	private ISysUserRoleService sysUserRoleService;

	private SysUser sysUser = new SysUser();
	
	//private SysRole sysRole=new SysRole();
	
	//private SysUserRole sysUserRole = new SysUserRole();
	
	private SysUserQuery baseQuery = new SysUserQuery();
	
	private PageList pageList;
	
	private List<SysUserResponse> data=new ArrayList<>();
	
	public void setSysUserService(ISysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}
	
	public void setSysUserRoleService(ISysUserRoleService sysUserRoleService) {
		this.sysUserRoleService = sysUserRoleService;
	}
	@Override
	protected void list() {
		/**
		 * 如果局点不为空，拿到登录进去的局点或者切换的局点
		 * 如果为空就拿到然后赋值给局点这个变量
		 */
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		
		Map<Long, SysRole> sysRoleMap=new HashMap<>();
		sysRoleService.getSysRoleMap(sysRoleMap, null);
		Map<Long, SysUserRole> sysUserRoleMap=new HashMap<>();
		sysUserRoleService.getSysUserRoleMap(sysUserRoleMap);
		Map<String, SysCode> nameCodeMap=new HashMap<>();
		Map<Long, SysCode> map=new HashMap<>();
		sysCodeService.getSysCodeMap(map, nameCodeMap);//得到sName对应SysCode的map
		Long iCompany= nameCodeMap.get(companyName).getiId();
		baseQuery.setiCompany(iCompany);
		this.pageList = sysUserService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				SysUser sysUser=(SysUser) pageList.getRows().get(i);
				SysUserResponse response=new SysUserResponse(sysUser);
				response.setCompanyName(map.get(sysUser.getiCompany()).getsName());
				
				
				SysUserRole sysUserRole=sysUserRoleMap.get(sysUser.getiId());
				if(sysUserRole==null){
					//response.setRoleId(-1L);
					response.setRoleName("");
				}else {
					//response.setRoleId(sysUserRole.getiRole());
					response.setRoleName(sysRoleMap.get(sysUserRole.getiRole()).getsName());
				}
				
				data.add(response);
			}
		}
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public void validateSave() {
		if (StringUtils.isBlank(sysUser.getsUserNo())) {
			addFieldError("name", "名称不能为空");
		}
	}

	// 处理保存和修改方法
	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		//  新增后的保存
		if(id==null){
			// 保存后添加到最后一页
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		// sysUserService.saveorUpdate(sysUser);
		return RELOAD;
	}

	// 删除
	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				sysUserService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	// input前置方法
	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			sysUser = sysUserService.get(id);// 变成栈顶对象
		}
	}

	// save前置方法
	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			// 新增后的保存
			sysUser = new SysUser();
		} else {
			sysUser = sysUserService.get(id);
		}
	}

	// 圧栈后院获取
	@Override
	public SysUser getModel() {
		return sysUser;
	}

	public PageList getPageList() {
		return pageList;
	}

	public SysUserQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(SysUserQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<SysUserResponse> getData() {
		return data;
	}

	public void setData(List<SysUserResponse> data) {
		this.data = data;
	}
	
	
}
